<?php

namespace Home\Controller;
use COM\Page;

/**
 * Created by PhpStorm.
 * User: chuanbin
 * Date: 2018/12/25
 * Time: 15:50
 */
class DailyController extends HomeController
{

    /**
     * 首页
     */
    public function active(){
        $uid = is_login();
        if($uid<=0){
            $this->mobileApi(null,'请登录后签到',-403);
        } else {
            //获取用户当天的签到状态
            $obj = M('active');
            //今天最早的签到时间
            $map['date_str'] = date('Y-m-d');
            $map['uid'] = is_login();
            $map['active_type'] = 1;
//            $map
            $info['on'] = $obj->where($map)->min('active_time');
            $map['active_type'] = 2;
            $info['off'] = $obj->where($map)->max('active_time');
            $this->assign('info',$info);
            $this->assign('li_nav',1);
            $this->display();
        }
    }

    /**
     * 日报首页
     */
    public function report(){
        $this->assign('li_nav',2);
        $this->display();
    }

    /**
     * 日报列表
     */
    public function report_list(){
        //查询用户的日报记录
        $uid = is_login();

        $User = M('report'); // 实例化User对象
// 进行分页数据查询 注意page方法的参数的前面部分是当前的页数使用 $_GET[p]获取
        $list = $User->where('uid='.$uid)->order('create_time')->page(intval($_GET['p']).',30')->select();
        $this->assign('list',$list);// 赋值数据集
        $count      = $User->where('uid='.$uid)->count();// 查询满足要求的总记录数
        $Page       = new \COM\Page($count,30);// 实例化分页类 传入总记录数和每页显示的记录数
        $show       = $Page->show();// 分页显示输出
        $this->assign('page',$show);// 赋值分页输出
        $this->assign('li_nav',4);
        $this->display(); // 输出模板
    }

    public function report_detail(){
        $id = I('id',0,'intval');
        if($id<=0){
            $this->error('详情错误');
        } else {
            $obj = M('report');
            $map['id'] = $id;
            $info = $obj->where($map)->find();
            $this->assign('info',$info);
            $this->assign('li_nav',4);
            $this->display();
        }
    }

    /**
     * 保存日报
     */
    public function save_report(){
        $uid = is_login();
        if($uid<=0){
            $this->mobileApi(null,'请登录后签到',-403);
        } else {
            $title = I('title','','trim');
            $content = I('content','','trim');
            if(empty($title)||empty($content)){
                $this->error('内容不能为空！');
            }
            $obj = M('report');
            $obj->uid = $uid;
            $obj->title = $title;
            $obj->content = $content;
            $obj->date_str = date('Y-m-d');
            $obj->create_time = time();
            $obj->name = $_SESSION['onethink_home']['user_auth']['username'];
            $id = $obj->add();
            if($id>0){
                $this->success('提交成功');
            } else {
                $this->success('提交失败，请联系管理员');
            }
        }
    }

    /**
     * 签到
     */
    public function do_active(){
        $uid = is_login();
        if($uid<=0){
            $this->mobileApi(null,'请登录后签到',-403);
        } else {
            $obj = M('active');
            $obj->uid = $uid;
            $obj->active_type = 1;
            $obj->date_str = date('Y-m-d');
            $obj->active_time = time();
            $obj->ip = get_client_ip(0);
            $obj->name = $_SESSION['onethink_home']['user_auth']['username'];
            $id = $obj->add();
            if($id>0){
                $this->mobileApi(date('Y-m-d H:i:s'),'签到成功',200);
            } else {
                $this->mobileApi($id,'签到失败，请联系管理员',404);
            }
        }
    }

    /**
     * 签退
     */
    public function off_active(){
        $uid = is_login();
        if($uid<=0){
            $this->mobileApi(null,'请登录后签退',-403);
        } else {
            //校验当天是否有签退记录 如果有就更新记录

            $obj = M('active');
            $map['uid'] = $uid;
            $map['active_type'] = 2;
            $map['date_str'] = date('Y-m-d');
            $id = $obj->where($map)->getField('id');
            if($id>0){
                $set['active_time'] = time();
                $set['ip'] = get_client_ip(0);
                $obj->where($map)->save($set);
            } else {
                $obj->uid = $uid;
                $obj->active_type = 2;
                $obj->date_str = date('Y-m-d');
                $obj->active_time = time();
                $obj->ip = get_client_ip(0);
                $obj->name = $_SESSION['onethink_home']['user_auth']['username'];
                $id = $obj->add();
            }

            if($id>0){
                $this->mobileApi(date('Y-m-d H:i:s'),'签退成功',200);
            } else {
                $this->mobileApi($id,'签退失败，请联系管理员',404);
            }
        }
    }

    /**
     * 考勤列表
     */
    public function active_list(){
        //查询用户的日报记录
        $uid = is_login();

        $obj = M('active'); // 实例化User对象
//        $obj->where('uid='.$uid)->order('active_time')->page(intval($_GET['p']).',30')->select();
// 进行分页数据查询 注意page方法的参数的前面部分是当前的页数使用 $_GET[p]获取

        //模拟一个万年历
        $year=date('Y');
        //如果没有传入月份则获取当前系统月份
        $month=date('m');

        //获取当前月有多少天
        $days=date('t',strtotime("{$year}-{$month}-1"));
        //当前1号是星期几
        $week=date('w',strtotime("{$year}-{$month}-1"));


        //铺表格
        $z = 0;

        $list = $obj->where('uid='.$uid)->order('active_time')->select();

        foreach($list as $key=>$value){
            if($value['active_type']==1){
                $u_date[$value['date_str']][1] = $value['active_time'];
            } else {
                $u_date[$value['date_str']][2] = $value['active_time'];
            }

        }

//        print_r($u_date);die;

        for($i=1-$week;$i<=$days;){
//            echo "<tr>";
            for($j=0;$j<7;$j++){
                if($i>$days || $i<=0){
//                    echo "<td>&nbsp;</td>";
                    $data[$z][$i]['text'] = '';
                    $data[$z][$i]['j'] = $j;
                }else{
                    $data[$z][$i]['text'] = $i;
                    $data[$z][$i]['j'] = $j;
                    //对应关系
                    if($i<10){
                        $date_str = $year.'-'.$month.'-0'.$i;
                    } else {
                        $date_str = $year.'-'.$month.'-'.$i;
                    }
                    $data[$z][$i]['on'] = $u_date[$date_str][1];
                    $data[$z][$i]['off'] = $u_date[$date_str][2];

//                    echo "<td>{$i}<br/> 上班 9：00 下班 19：30</td>";
                }
                $i++;
            }
            $z++;
//            echo "</tr>";
        }

        $this->assign('table',$data);
        $this->assign('year',$year);
        $this->assign('month',$month);

//        print_r($data);die;




        $list = $obj->where('uid='.$uid)->order('active_time')->page(intval($_GET['p']).',30')->select();

//        print_r($list);die;
        $this->assign('list',$list);// 赋值数据集
        $count      = $obj->where('uid='.$uid)->count();// 查询满足要求的总记录数
        $Page       = new \COM\Page($count,30);// 实例化分页类 传入总记录数和每页显示的记录数
        $show       = $Page->show();// 分页显示输出
        $this->assign('page',$show);// 赋值分页输出
        $this->assign('li_nav',3);
        $this->display(); // 输出模板
    }
}